package graphics.rasterizing.projection;

import static util.MatrixOperationsF.ar;
import static util.MatrixOperationsF.cloo;
import static util.MatrixOperationsF.multiply;
import static util.Transformations.generalOrthographicProjection;
import static util.Transformations.ones;

public class PerspectiveProjection implements Projection {

	private float[] _lbn;
	private float[] _rtf;

	public PerspectiveProjection(float[] lbn, float[] rtf) {
		_lbn = cloo(lbn);
		_rtf = cloo(rtf);
	}

	@Override
	public float[][] getMatrix() {
		float[][] rv = ones(4);
		rv[0][0] = _lbn[2];
		rv[1][1] = _lbn[2];
		rv[2][2] = _lbn[2] + _rtf[2];
		rv[3][3] = 0;
		rv[3][2] = 1;
		rv[2][3] = -_lbn[2] * _rtf[2];
		float[] rtf = multiply(rv, _rtf);
		float[][] ortho = generalOrthographicProjection(_lbn,
				ar(_rtf[0], _rtf[1], rtf[2]));
		rv = multiply(ortho, rv);
		return rv;
	}

}
